﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Table.DataModel;
using UnityEngine.UI;
using Table;

namespace UI
{
    public class AutoChessExplainPageHeroBuff : MonoBehaviour
    {

        public GameObject selectedIcon;
        public GroupSpriteKit buffSpriteKit;
        public GameObject detailPanelMask;
        public Transform detailPanelTransformTop;
        public Transform detailPanelTransformBottom;
        public Transform detailPanelArrowTransTop;
        public Transform detailPanelArrowTransBottom;
        public Transform detailPanelParent;
        public Transform viewPortTransform;
        public GameObject buffDetailPanel;
        public GameObject buffDetailPanelArrow;
        public ScrollRect scrollRect;
        // 阵营或者职业名
        public Text groupName;
        public Text groupDesc;
        public GroupSpriteKit detailPanelBuffIcon;
        public RectTransform groupItemsParent;
        public GameObject multiplyItem;

        //private bool hasContentInit = false;
        private TableAutoChessGroup groupData;
        // 所有的 buff id
        //private int[] buffIds;

        public void SetContent(TableAutoChessGroup group)
        {
            groupData = group;
            buffSpriteKit.SetSpriteName(groupData.altasName);

        }

        public void OnClick()
        {
            detailPanelMask.SetActive(true);
            scrollRect.velocity = Vector2.zero;
            scrollRect.vertical = false;
            selectedIcon.SetActive(true);

            // buff 详情面板的 buff 累计
            if (groupData.buffId != null)
            {
                // 阵营/职业图标
                detailPanelBuffIcon.SetSpriteName(groupData.altasName);
                // 阵营/职业名称
                groupName.text = groupData.groupSkillName;
                groupDesc.text = groupData.desc;
                string[] subStrings = groupData.buffId.Split(',',';');

                if (groupItemsParent.transform.childCount > 1)
                {
                    // 第一个是顶部的 buff 信息，不清理
                    for (int i = 1; i < groupItemsParent.childCount; i++)
                    {
                        Destroy(groupItemsParent.GetChild(i).gameObject);
                    }
                }

                for (int i = 0; i < subStrings.Length; i++)
                {
                    int buffId = int.Parse(subStrings[i]);
                    TableautoChessGroupBuffDesc buffData = TableDataManager.Instance.tableautoChessGroupBuffDescDao.GetById(buffId);
                    if (buffData == null)
                    {
                        ExLog.LogError("表 autoChessGroupBuffDesc 中不存在 id 为 " + buffId + " 的 buff 数据！");
                        continue;
                    }
                    GameObject buffItem = Instantiate(multiplyItem, groupItemsParent);
                    AutoChessExplainPageHeroBuffItem item = buffItem.GetComponent<AutoChessExplainPageHeroBuffItem>();
                    if (item != null)
                    {
                        item.InitContent(buffData.needHeroNum, buffData.desc);
                    }
                    buffItem.SetActive(true);
                }

            }

            detailPanelTransformBottom.SetParent(detailPanelParent);

            float BottomRectTransformPosY = detailPanelTransformBottom.GetComponent<RectTransform>().anchoredPosition.y;
            float skillDetailPanelHeight = buffDetailPanel.GetComponent<RectTransform>().rect.height;
            float skillDetailPanelParentBottom = detailPanelParent.GetComponent<RectTransform>().rect.height / 2f;

            // 面板应该出现在图标的底部吗？
            bool isBottom = BottomRectTransformPosY - skillDetailPanelHeight > -skillDetailPanelParentBottom;

            if (isBottom)
            {
                groupItemsParent.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0f, groupItemsParent.rect.width);
                groupItemsParent.pivot = new Vector3(0f, 1f);
                groupItemsParent.anchoredPosition = new Vector2(groupItemsParent.anchoredPosition.x, -12.7f);

                buffDetailPanel.transform.localPosition = detailPanelTransformBottom.localPosition;
                detailPanelTransformBottom.SetParent(viewPortTransform);

                detailPanelArrowTransBottom.SetParent(buffDetailPanel.transform);
                buffDetailPanelArrow.transform.localPosition = detailPanelArrowTransBottom.localPosition;
                buffDetailPanelArrow.transform.localRotation = Quaternion.identity;
                detailPanelArrowTransBottom.SetParent(transform);
            }
            else
            {
                groupItemsParent.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0f, groupItemsParent.rect.width);
                groupItemsParent.pivot = new Vector3(0f, 0f);
                groupItemsParent.anchoredPosition = new Vector2(groupItemsParent.anchoredPosition.x, 0.6f);

                detailPanelTransformTop.SetParent(detailPanelParent);
                buffDetailPanel.transform.localPosition = detailPanelTransformTop.localPosition;
                buffDetailPanel.transform.localPosition += new Vector3(0f, skillDetailPanelHeight, 0f);
                detailPanelTransformTop.SetParent(viewPortTransform);

                detailPanelArrowTransTop.SetParent(buffDetailPanel.transform);
                buffDetailPanelArrow.transform.localPosition = detailPanelArrowTransTop.localPosition;
                buffDetailPanelArrow.transform.localEulerAngles = new Vector3(180f, 0f, 0f);
                detailPanelArrowTransTop.SetParent(transform);
            }
            detailPanelTransformBottom.SetParent(viewPortTransform);

            buffDetailPanel.SetActive(true);

            //buffDetailPanel.transform.localPosition = transform.localPosition + transform.parent.transform.localPosition;
            // 添加隐藏说明面板的委托
            AutoChessHerosSubPage.OnHideDetailPanel = () =>
            {
                selectedIcon.SetActive(false);
            };
        }
    }
}
